package com.cats.utility;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.cats.event.LoginEvent;
import com.cats.interfaces.ILoginListener;

public class AccountManageUtility {
	private static boolean loggedIn = false;
	private static final String LOGIN_USERNAME = "login_username";
	private static final String LOGIN_PASSWORD = "login_password";
	private static SharedPreferences preferences = null;
	private static Editor editor = null;

	@SuppressWarnings("rawtypes")
	private List loginEventListeners = new ArrayList();

	private static AccountManageUtility sharedInstance = null;
	
	public static AccountManageUtility getSharedAccountManager(Context context) {
		if (sharedInstance == null) {
			sharedInstance = new AccountManageUtility();
		}
		
		preferences = PreferenceManager.getDefaultSharedPreferences(context);
		editor = preferences.edit();
		
		return sharedInstance;
	}
	
	private AccountManageUtility() {
		
	}

	public boolean isUserLogin() {
		return loggedIn;
	}

	// TODO:
	public void doLogIn(String username, String password) {
		loggedIn = true;

		// TODO: after login success, to this
		editor.putString(LOGIN_USERNAME, username);
		editor.putString(LOGIN_PASSWORD, password);
		editor.commit();
	}
	
	// TODO:
	public void doAutoLogin() {
		String usernameString = preferences.getString(LOGIN_USERNAME, null);
		String passwordString = preferences.getString(LOGIN_PASSWORD, null);
		if (usernameString != null && passwordString != null) {
			doLogIn(usernameString, passwordString);
			
			// TODO: if success, notify listeners
			notifyLogInSuccess();
		}
		
		//test 
		notifyLogInFailed();
	}

	// TODO:
	public void doSignOut() {
		loggedIn = false;
		editor.remove(LOGIN_USERNAME);
		editor.remove(LOGIN_PASSWORD);
		editor.commit();
	}

	/**
	 * if find username and password both saved, return true, oherwise false
	 * 
	 * @return
	 */
	// TODO:
	public boolean isAccountSaved() {
		if (preferences.getString(LOGIN_USERNAME, null) != null
				&& preferences.getString(LOGIN_PASSWORD, null) != null) {
			return true;
		}

		return true;
	}

	private void notifyLogInSuccess() {
		LoginEvent le = new LoginEvent(this, LoginEvent.SUCCESS);
	    Iterator i = loginEventListeners.iterator();
	    while(i.hasNext())  {
	      ((ILoginListener) i.next()).handleLoginEvent(le);
	    }
		
	}

	private void notifyLogInFailed() {
		LoginEvent le = new LoginEvent(this, LoginEvent.FAILED);
	    Iterator i = loginEventListeners.iterator();
	    while(i.hasNext())  {
	      ((ILoginListener) i.next()).handleLoginEvent(le);
	    }

	}
	
	@SuppressWarnings("unchecked")
	public synchronized void addLoginEventListener(ILoginListener listener) {
	    loginEventListeners.add(listener);
	}
	  
	public synchronized void removeLoginEventListener(ILoginListener listener) {
		loginEventListeners.remove(listener);
	}
}
